package com.ltkj.shop.service.impl;

import java.util.List;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.datascope.annotation.DataScope;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.shop.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.shop.domain.ShopStore;
import com.ltkj.shop.service.IShopStoreService;

/**
 * 文创商店Service业务层处理
 * 
 * @author xupf
 * @date 2023-08-29
 */
@Service
public class ShopStoreServiceImpl implements IShopStoreService 
{
    @Autowired
    private ShopStoreMapper shopStoreMapper;
    @Autowired
    private ShopStoreBizMapper shopStoreBizMapper;
    @Autowired
    private ShopGoodsBizMapper shopGoodsBizMapper;
    @Autowired
    private ShopStoreCentersBizMapper shopStoreCentersBizMapper;

    /**
     * 查询文创商店
     * 
     * @param id 文创商店主键
     * @return 文创商店
     */
    @Override
    public ShopStore selectShopStoreById(Long id)
    {
        return shopStoreMapper.selectShopStoreById(id);
    }

    /**
     * 查询文创商店列表
     * 
     * @param shopStore 文创商店
     * @return 文创商店
     */
    @Override
    @DataScope(storeAlias = "s")
    public List<ShopStore> selectShopStoreList(ShopStore shopStore)
    {
        return shopStoreBizMapper.selectShopStoreList(shopStore);
    }

    /**
     * 新增文创商店
     * 
     * @param shopStore 文创商店
     * @return 结果
     */
    @Override
    public ShopStore insertShopStore(ShopStore shopStore)
    {
        shopStore.setCreateTime(DateUtils.getNowDate());
        shopStore.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        shopStore.setUserId(SecurityUtils.getLoginUser().getUserid());
        shopStore.setIsDel("0");
        shopStoreMapper.insertShopStore(shopStore);
        return shopStore;
    }

    /**
     * 修改文创商店
     * 
     * @param shopStore 文创商店
     * @return 结果
     */
    @Override
    public int updateShopStore(ShopStore shopStore)
    {
        shopStore.setUpdateTime(DateUtils.getNowDate());
        shopStore.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return shopStoreMapper.updateShopStore(shopStore);
    }

    /**
     * 通过商店ID查询商店上架数量
     * @param storeId 商店ID
     * @return
     */
    @Override
    public int countGoodsByStoreId(Long storeId, String status) {
        return shopGoodsBizMapper.countGoodsByStoreId(storeId, status);
    }

    /**
     * 批量删除文创商店
     * 
     * @param ids 需要删除的文创商店主键
     * @return 结果
     */
    @Override
    public int deleteShopStoreByIds(Long[] ids)
    {
        for (Long id : ids) {
            if (countGoodsByStoreId(id, null) > 0)
            {
                throw new ServiceException("商店已有上架商品,不能删除");
            }
        }
        // 删除商品和自提点关联关系
        shopStoreCentersBizMapper.deleteShopStoreCenters(ids, null);
        return shopStoreBizMapper.deleteShopStoreByIds(ids);
    }

    /**
     * 删除文创商店信息
     * 
     * @param id 文创商店主键
     * @return 结果
     */
    @Override
    public int deleteShopStoreById(Long id)
    {
        // 删除商品和自提点关联关系
        shopStoreCentersBizMapper.deleteShopStoreCenters(new Long[]{id}, null);
        return shopStoreBizMapper.deleteShopStoreById(id);
    }
}
